package ru.spb.osll.myfit.client.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import ru.spb.osll.myfit.client.Mode;
import ru.spb.osll.myfit.client.log.RemoteLogger;
import ru.spb.osll.myfit.client.service.CreateHelperService;
import ru.spb.osll.myfit.client.service.CreateHelperServiceAsync;
import ru.spb.osll.myfit.common.PageTag;
import ru.spb.osll.myfit.common.Suite;

import java.util.List;

public class SuiteWidget extends Composite {
  private VerticalPanel m_widget;
  private TextBox m_rootTextBox = new TextBox();
  private SuggestBox m_rootBox;
  private TagPanelWidget m_tagPanelWidget = new TagPanelWidget();
  private SuiteTestsWidget m_testsList;
  private MultiWordSuggestOracle m_oracle = new MultiWordSuggestOracle(".");

  public SuiteWidget() {
    m_widget = new VerticalPanel();
    m_widget.setStyleName(IStyleConstants.TABLE_PANEL_STYLE);
    m_widget.setSpacing(20);

    final CreateHelperServiceAsync createHelperService = GWT.create(CreateHelperService.class);

    createHelperService.loadPageNames(new AsyncCallback<List<String>>() {
      @Override
      public void onFailure(Throwable throwable) {
        RemoteLogger.error("Error submitting page names list");
      }

      @Override
      public void onSuccess(List<String> names) {
        m_oracle.addAll(names);
        m_oracle.setDefaultSuggestionsFromText(names);
      }
    });

    initWidget(m_widget);
  }

  private void updateTestsList(Suite s) {
    Suite suite = new Suite(s.getId());
    suite.setRoot(m_rootTextBox.getText());
    suite.setTags(m_tagPanelWidget.getTags());
    m_testsList.update(suite);
  }

  public void setSuite(final Suite suite) {
    HorizontalPanel rootPanel = new HorizontalPanel();
    rootPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
    rootPanel.add(new Label("Root:"));
    HorizontalPanel separator = new HorizontalPanel();
    separator.setWidth("6");
    rootPanel.add(separator);

    m_rootTextBox.setReadOnly(true);
    m_rootTextBox.setText(suite.getRoot());

    m_rootBox = new SuggestBox(m_oracle, m_rootTextBox);
    m_rootBox.setWidth("250px");

    rootPanel.add(m_rootBox);
    m_widget.add(rootPanel);

    HorizontalPanel tagsPanel = new HorizontalPanel();
    tagsPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
    tagsPanel.add(new Label("Tags:"));
    HorizontalPanel wideSeparator = new HorizontalPanel();
    wideSeparator.setWidth("8");
    tagsPanel.add(wideSeparator);
    tagsPanel.add(m_tagPanelWidget);
    m_widget.add(tagsPanel);

    m_tagPanelWidget.setTags(suite.getTags());

    m_testsList = new SuiteTestsWidget();
    m_testsList.update(suite);
    m_widget.add(m_testsList);

    m_tagPanelWidget.setEventHandler(new TagWidgetEventHandler() {
      @Override
      public void onTagRemove(PageTag tag) {
        updateTestsList(suite);
      }

      @Override
      public void onTagAdd(PageTag tag) {
        updateTestsList(suite);
      }
    });

    m_rootTextBox.addChangeHandler(new ChangeHandler() {
      @Override
      public void onChange(ChangeEvent changeEvent) {
        updateTestsList(suite);
      }
    });
  }

  public Suite getSuite() {
    Suite suite = new Suite();
    suite.setRoot(m_rootTextBox.getText());
    suite.setTags(m_tagPanelWidget.getTags());
    return suite;
  }

  public boolean addTag(String tag) {
    return m_tagPanelWidget.createNewTag(tag);
  }

  public void setMode(Mode mode) {
    m_tagPanelWidget.setEnabled(mode == Mode.EDIT);
    m_rootTextBox.setReadOnly(!(mode == Mode.EDIT));
  }
}
